﻿#include "stdafx.h"
#include "S_MonitorWindowManager.h"
#include "S_MonitorDataContainer.h"

#include "DSEMUI.h"
#include "Source/ProjectModule/S_ProjectManager.h"

#include "Source/DisplayAdapterModule/S_DisplayAdapterGetter.h"
#include "Source/ModelingPreModule/S_ModelingPre_InteractiveManager.h"

/*
-----==========================================================-----
		类：		场强探头模块 - 窗口管理.cpp
		作者：		drill_up
		所属模块：	场强探头模块
		功能：		对打开、编辑窗口进行操作，包括响应相关控制。
					（详细见cpp）
-----==========================================================-----
*/

S_MonitorWindowManager::S_MonitorWindowManager() {
}
S_MonitorWindowManager::~S_MonitorWindowManager() {
}

/* --------------------------------------------------------------
----------单例
*/
S_MonitorWindowManager* S_MonitorWindowManager::cur_manager = NULL;
S_MonitorWindowManager* S_MonitorWindowManager::getInstance() {
	if (cur_manager == NULL) {
		cur_manager = new S_MonitorWindowManager();
	}
	return cur_manager;
}

/* --------------------------------------------------------------
		场监视器 - 添加
*/
void S_MonitorWindowManager::openAddWindowFieldMonitor() {
	this->m_W_FieldMonitor = new W_FieldMonitor(DSEMUI::getInstance());
	connect(this->m_W_FieldMonitor, &W_FieldMonitor::acceptDataSignal, this, &S_MonitorWindowManager::acceptDataFieldMonitor);
	this->m_W_FieldMonitor->setDataInAddMode();

	// > 添加前，如果已选择了点，那么自动填写
	QStringList id_list = S_ModelingPre_InteractiveManager::getInstance()->pickList_point_getAllId();
	if (id_list.count() == 1) {
		QList<double> p1 = S_ModelingPre_InteractiveManager::getInstance()->pickList_point_getDataById(id_list.at(0));
		QList<double> p2;
		QList<double> p3;
		this->m_W_FieldMonitor->setDataPoint(p1, p2, p3);
	}
	if (id_list.count() == 2) {
		QList<double> p1 = S_ModelingPre_InteractiveManager::getInstance()->pickList_point_getDataById(id_list.at(0));
		QList<double> p2 = S_ModelingPre_InteractiveManager::getInstance()->pickList_point_getDataById(id_list.at(1));
		QList<double> p3;
		this->m_W_FieldMonitor->setDataPoint(p1, p2, p3);
	}
	if (id_list.count() >= 3) {
		QList<double> p1 = S_ModelingPre_InteractiveManager::getInstance()->pickList_point_getDataById(id_list.at(id_list.count() - 3));
		QList<double> p2 = S_ModelingPre_InteractiveManager::getInstance()->pickList_point_getDataById(id_list.at(id_list.count() - 2));
		QList<double> p3 = S_ModelingPre_InteractiveManager::getInstance()->pickList_point_getDataById(id_list.at(id_list.count() - 1));
		this->m_W_FieldMonitor->setDataPoint(p1, p2, p3);
	}

	// 通过设置一个非常小的值，此时就会依据policy去自动放置到一个可以容纳全部组件的尺寸
	this->m_W_FieldMonitor->resize(10, 10);
	// this->m_W_FieldMonitor->resize(S_DisplayAdapterGetter::getInstance()->getAppWidth() / 2, S_DisplayAdapterGetter::getInstance()->getAppHeight());
	
	this->m_W_FieldMonitor->show();	//（关闭后自动销毁）
}
/* --------------------------------------------------------------
		场监视器 - 编辑
*/
void S_MonitorWindowManager::openModifyWindowFieldMonitor(QString id) {
	this->m_W_FieldMonitor = new W_FieldMonitor(DSEMUI::getInstance());
	connect(this->m_W_FieldMonitor, &W_FieldMonitor::acceptDataSignal, this, &S_MonitorWindowManager::acceptDataFieldMonitor);
	C_FieldMonitor data = S_MonitorDataContainer::getInstance()->getDataFieldMonitorById(id);
	this->m_W_FieldMonitor->setDataInModifyMode(data);

	// 通过设置一个非常小的值，此时就会依据policy去自动放置到一个可以容纳全部组件的尺寸
	this->m_W_FieldMonitor->resize(10, 10);
	// this->m_W_FieldMonitor->resize(S_DisplayAdapterGetter::getInstance()->getAppWidth() / 2, S_DisplayAdapterGetter::getInstance()->getAppHeight());
	
	this->m_W_FieldMonitor->show();	//（关闭后自动销毁）
}
/* --------------------------------------------------------------
		场监视器 - 编辑（action）
*/
void S_MonitorWindowManager::openModifyWindowFieldMonitorInAction() {
	QAction* cur_action = qobject_cast<QAction*>(this->sender());		//从action里面取出数据指针
	QString id = cur_action->data().value<QString>();
	this->openModifyWindowFieldMonitor(id);
}
/* --------------------------------------------------------------
		场监视器 - 提交数据
*/
void S_MonitorWindowManager::acceptDataFieldMonitor(bool isAddMode, C_FieldMonitor data) {
	S_ProjectManager::getInstance()->setDirty();
	if (isAddMode == true) {
		if (data.getSpecType() == 1){
			// 时域
			S_MonitorDataContainer::getInstance()->add(data);
		}
		else
		{
			// 提交的时候，通过freqs进行拆分，生成一个list，然后在提交的函数中遍历添加
			QStringList freqs = data.getFreqs();
			if (freqs.count() > 0)
			{
				for (int i = 0; i < freqs.count(); ++i)
				{
					C_FieldMonitor tmp_data = data.clone();
					tmp_data.generateRandomId();
					tmp_data.setFreq(TTool::_to_double_e_(freqs[i]));
					QString name = QString("%1  (f=%2)").arg(tmp_data.getMonitorTypeString()).arg(freqs[i]);
					tmp_data.setName(name);
					S_MonitorDataContainer::getInstance()->add(tmp_data);
				}
			}
			else
			{
				S_MonitorDataContainer::getInstance()->add(data);
			}
		}
	}
	else {
		// 修改的时候只有一个
		S_MonitorDataContainer::getInstance()->modify(data);
	}
}



/* --------------------------------------------------------------
		场强探头 - 添加
*/
void S_MonitorWindowManager::openAddWindowFieldProbe() {
	this->m_W_FieldProbe = new W_FieldProbe(DSEMUI::getInstance());
	connect(this->m_W_FieldProbe, &W_FieldProbe::acceptDataSignal, this, &S_MonitorWindowManager::acceptDataFieldProbe);
	this->m_W_FieldProbe->setDataInAddMode();

	// > 添加前，如果已选择了点，那么自动填写
	QStringList id_list = S_ModelingPre_InteractiveManager::getInstance()->pickList_point_getAllId();
	if (id_list.count() >= 1) {
		QList<double> p1 = S_ModelingPre_InteractiveManager::getInstance()->pickList_point_getDataById(id_list.last());
		this->m_W_FieldProbe->setDataPoint(p1);
	}

	this->m_W_FieldProbe->refreshModel();
	this->m_W_FieldProbe->show();	//（关闭后自动销毁）
}
/* --------------------------------------------------------------
		场强探头 - 编辑
*/
void S_MonitorWindowManager::openModifyWindowFieldProbe(QString id) {
	this->m_W_FieldProbe = new W_FieldProbe(DSEMUI::getInstance());
	connect(this->m_W_FieldProbe, &W_FieldProbe::acceptDataSignal, this, &S_MonitorWindowManager::acceptDataFieldProbe);
	C_FieldProbe fp = S_MonitorDataContainer::getInstance()->getDataFieldProbeById(id);
	this->m_W_FieldProbe->setDataInModifyMode(fp);

	// > 编辑前，清除当前的模型
	S_MonitorDisplayingManager::getInstance()->clearModel_NoUpdate_Probe(id);
	S_ModelingPre_InteractiveManager::getInstance()->updateViewModeling();

	this->m_W_FieldProbe->show();	//（关闭后自动销毁）
}
/* --------------------------------------------------------------
		场强探头 - 编辑（action）
*/
void S_MonitorWindowManager::openModifyWindowFieldProbeInAction() {
	QAction* cur_action = qobject_cast<QAction*>(this->sender());		//从action里面取出数据指针
	QString id = cur_action->data().value<QString>();
	this->openModifyWindowFieldProbe(id);
}
/* --------------------------------------------------------------
		场强探头 - 提交数据
*/
void S_MonitorWindowManager::acceptDataFieldProbe(bool isAddMode, C_FieldProbe data) {
	S_ProjectManager::getInstance()->setDirty();
	if (isAddMode == true) {
		S_MonitorDataContainer::getInstance()->add(data);
	}
	else {
		S_MonitorDataContainer::getInstance()->modify(data);
	}
}



/* --------------------------------------------------------------
		电压监视器 - 添加
*/
void S_MonitorWindowManager::openAddWindowVoltageMonitor() {
	this->m_W_VoltageMonitor = new W_VoltageMonitor(DSEMUI::getInstance());
	connect(this->m_W_VoltageMonitor, &W_VoltageMonitor::acceptDataSignal, this, &S_MonitorWindowManager::acceptDataVoltageMonitor);
	this->m_W_VoltageMonitor->setDataInAddMode();

	// > 添加前，如果已选择了点，那么自动填写
	QStringList id_list = S_ModelingPre_InteractiveManager::getInstance()->pickList_point_getAllId();
	if (id_list.count() == 1) {
		QList<double> p1 = S_ModelingPre_InteractiveManager::getInstance()->pickList_point_getDataById(id_list.at(0));
		QList<double> p2;
		this->m_W_VoltageMonitor->setDataPoint(p1, p2);
	}
	if (id_list.count() >= 2) {
		QList<double> p1 = S_ModelingPre_InteractiveManager::getInstance()->pickList_point_getDataById(id_list.at(id_list.count() - 2));
		QList<double> p2 = S_ModelingPre_InteractiveManager::getInstance()->pickList_point_getDataById(id_list.at(id_list.count() - 1));
		this->m_W_VoltageMonitor->setDataPoint(p1, p2);
	}
	
	this->m_W_VoltageMonitor->show();	//（关闭后自动销毁）
}
/* --------------------------------------------------------------
		电压监视器 - 编辑
*/
void S_MonitorWindowManager::openModifyWindowVoltageMonitor(QString id) {
	this->m_W_VoltageMonitor = new W_VoltageMonitor(DSEMUI::getInstance());
	connect(this->m_W_VoltageMonitor, &W_VoltageMonitor::acceptDataSignal, this, &S_MonitorWindowManager::acceptDataVoltageMonitor);
	this->m_W_VoltageMonitor->setDataInModifyMode(S_MonitorDataContainer::getInstance()->getDataVoltageMonitorById(id));

	// > 编辑前，清除当前的模型
	S_MonitorDisplayingManager::getInstance()->clearModel_NoUpdate_VoltageMonitor(id);
	S_ModelingPre_InteractiveManager::getInstance()->updateViewModeling();

	this->m_W_VoltageMonitor->show();	//（关闭后自动销毁）
}
/* --------------------------------------------------------------
		电压监视器 - 编辑（action）
*/
void S_MonitorWindowManager::openModifyWindowVoltageMonitorInAction() {
	QAction* cur_action = qobject_cast<QAction*>(this->sender());		//从action里面取出数据指针
	QString id = cur_action->data().value<QString>();
	this->openModifyWindowVoltageMonitor(id);
}
/* --------------------------------------------------------------
		电压监视器 - 提交数据
*/
void S_MonitorWindowManager::acceptDataVoltageMonitor(bool isAddMode, C_VoltageMonitor data){
	S_ProjectManager::getInstance()->setDirty();
	if (isAddMode == true) {
		S_MonitorDataContainer::getInstance()->add(data);
	}else {
		S_MonitorDataContainer::getInstance()->modify(data);
	}
}



/* --------------------------------------------------------------
		电流监视器 - 添加
*/
void S_MonitorWindowManager::openAddWindowCurrentMonitor() {
	this->m_W_CurrentMonitor = new W_CurrentMonitor(DSEMUI::getInstance());
	connect(this->m_W_CurrentMonitor, &W_CurrentMonitor::acceptDataSignal, this, &S_MonitorWindowManager::acceptDataCurrentMonitor);
	this->m_W_CurrentMonitor->setDataInAddMode();

	// > 添加前，如果已选择了点，那么自动填写
	QStringList id_list = S_ModelingPre_InteractiveManager::getInstance()->pickList_point_getAllId();
	if (id_list.count() == 1) {
		QList<double> p1 = S_ModelingPre_InteractiveManager::getInstance()->pickList_point_getDataById(id_list.at(0));
		QList<double> p2;
		QList<double> p3;
		this->m_W_CurrentMonitor->setDataPoint(p1, p2, p3);
	}
	if (id_list.count() >= 2) {
		QList<double> p1 = S_ModelingPre_InteractiveManager::getInstance()->pickList_point_getDataById(id_list.at(id_list.count() - 2));
		QList<double> p2 = S_ModelingPre_InteractiveManager::getInstance()->pickList_point_getDataById(id_list.at(id_list.count() - 1));
		QList<double> p3;
		this->m_W_CurrentMonitor->setDataPoint(p1, p2, p3);
	}
	if (id_list.count() >= 3) {
		QList<double> p1 = S_ModelingPre_InteractiveManager::getInstance()->pickList_point_getDataById(id_list.at(id_list.count() - 3));
		QList<double> p2 = S_ModelingPre_InteractiveManager::getInstance()->pickList_point_getDataById(id_list.at(id_list.count() - 2));
		QList<double> p3 = S_ModelingPre_InteractiveManager::getInstance()->pickList_point_getDataById(id_list.at(id_list.count() - 1));
		this->m_W_CurrentMonitor->setDataPoint(p1, p2, p3);
	}
	
	this->m_W_CurrentMonitor->show();	//（关闭后自动销毁）
}
/* --------------------------------------------------------------
		电流监视器 - 编辑
*/
void S_MonitorWindowManager::openModifyWindowCurrentMonitor(QString id) {
	this->m_W_CurrentMonitor = new W_CurrentMonitor(DSEMUI::getInstance());
	connect(this->m_W_CurrentMonitor, &W_CurrentMonitor::acceptDataSignal, this, &S_MonitorWindowManager::acceptDataCurrentMonitor);
	this->m_W_CurrentMonitor->setDataInModifyMode(S_MonitorDataContainer::getInstance()->getDataCurrentMonitorById(id));

	// > 编辑前，清除当前的模型
	S_MonitorDisplayingManager::getInstance()->clearModel_NoUpdate_CurrentMonitor(id);
	S_ModelingPre_InteractiveManager::getInstance()->updateViewModeling();

	this->m_W_CurrentMonitor->show();	//（关闭后自动销毁）
}
/* --------------------------------------------------------------
		电流监视器 - 编辑（action）
*/
void S_MonitorWindowManager::openModifyWindowCurrentMonitorInAction() {
	QAction* cur_action = qobject_cast<QAction*>(this->sender());		//从action里面取出数据指针
	QString id = cur_action->data().value<QString>();
	this->openModifyWindowCurrentMonitor(id);
}
/* --------------------------------------------------------------
		电流监视器 - 提交数据
*/
void S_MonitorWindowManager::acceptDataCurrentMonitor(bool isAddMode, C_CurrentMonitor data){
	S_ProjectManager::getInstance()->setDirty();
	if (isAddMode == true) {
		S_MonitorDataContainer::getInstance()->add(data);
	}else {
		S_MonitorDataContainer::getInstance()->modify(data);
	}
}